import <$ model.name $>Service from '../services/<$ model.name $>Service'
import * as responseTemplate from '../lib/responseTemplate'

export let get<$ model.Name $> = async (ctx) => {
    let id = ctx.query.id
    let <$ model.name $> = await <$ model.name $>Service.get<$ model.Name $>(id)
    if (!<$ model.name $>) {
        return responseTemplate.businessError(ctx, "<$ model.name $>不存在!")
    }
    return responseTemplate.success(ctx, post)
}
export let get<$ model.Name $>PagedList = async (ctx, next) => {
    let pageIndex = ctx.query.pageIndex
    let pageSize = ctx.query.pageSize
    let sortBy = ctx.query.sortBy
    let descending = ctx.query.descending
    let filter ={
        <% for property in model.properties %>
          <$ property.key $>:ctx.query.<$ property.key $>,
        <% endfor %>
    }
    let pagedList = await <$ model.name $>Service.get<$ model.Name $>PagedList(pageIndex, pageSize, sortBy, descending, filter)
    responseTemplate.success(ctx, pagedList)
    return next()
}
export let del<$ model.Name $> = async (ctx) => {
    let id = ctx.query.id
    await <$ model.name $>Service.del<$ model.Name $>(id)
    return responseTemplate.success(ctx, null)
}

export let del<$ model.Name $>s = async (ctx) => {
    let ids = JSON.parse(ctx.query.ids)
    for (let id of ids) {
        await <$ model.name $>Service.del<$ model.Name $>(id)
    }
    return responseTemplate.success(ctx, null)
}

export let save<$ model.Name $> = async (ctx) => {
    let entity = ctx.request.body
    <% for property in model.properties %>
      <% if property.key!='id' %>
        if(entity.<$ property.key $>==""){
          return responseTemplate.businessError(ctx, "<$ property.title $>不能为空!")
        }
      <% endif %>
    <% endfor %>
    let result = await <$ model.name $>Service.save<$ model.Name $>(entity)
    if (!result.success) {
        return responseTemplate.businessError(ctx, result.msg)
    }
    return responseTemplate.success(ctx, null)
}
